package com.example.yls.demoa;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity
implements IOnTimeListener{

    private Button startBtn,stopBtn,bindBtn,unbindBtn;
    private Button addBtn,resultBtn;
    private TextView dateText,date_broadcast_text;
    private Intent intent;
    private MyServiceConnection serviceConnection;

    Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
//            super.handleMessage(msg);
            String date = (String) msg.getData().get("date");
            dateText.setText("服务："+date);
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        intent = new Intent(MainActivity.this,MyService.class);
        serviceConnection = new MyServiceConnection();

        startBtn  = findViewById(R.id.start_service);
        stopBtn = findViewById(R.id.stop_service);
        bindBtn  = findViewById(R.id.bind_service);
        unbindBtn = findViewById(R.id.unbind_service);
        addBtn = findViewById(R.id.add_service);
        resultBtn = findViewById(R.id.result_service);
        dateText = findViewById(R.id.date_text);
        date_broadcast_text = findViewById(R.id.date_broadcast_text);

        MyBroadcastReceiver receiver = new MyBroadcastReceiver(this);
        IntentFilter filter = new IntentFilter();
        filter.addAction("TIME_CHANGED");
        registerReceiver(receiver,filter);

        resultBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                serviceConnection.getBind().send("Hello World!!", new IMyInterface() {
                    @Override
                    public void success(String msg) {
                        Log.e("MainActivity","服务回调");
                        Message msgs = new Message();
                        Bundle bundle = new Bundle();
                        bundle.putString("date",msg);

                        msgs.setData(bundle);
                        handler.sendMessage(msgs);
                    }

                    @Override
                    public void error() {

                    }
                });
            }
        });

        addBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int result = serviceConnection.getBind().add(100,200);
                Toast.makeText(MainActivity.this,"计算结果："+result,Toast.LENGTH_LONG).show();
            }
        });

        bindBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                bindService(intent,serviceConnection,BIND_AUTO_CREATE);
            }
        });

        unbindBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                serviceConnection.
                unbindService(serviceConnection);
            }
        });

        startBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startService(intent);
            }
        });

        stopBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                stopService(intent);
            }
        });

    }

    @Override
    public void setTime(String date) {
        date_broadcast_text.setText("广播："+date);
    }


}
